﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;
using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;

namespace Mengsk.ProxyDelivery.App.UI.Delivery
{
    /// <summary>
    /// ArriveInQueryUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class ArriveInQueryUserControl : UserControl, IPlugin
    {
        private ArriveInDeliveryService arriveInDeliveryService = ServiceContainer.GetService<ArriveInDeliveryService>();


        public string Title
        {
            get { return "收件查询"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        public ArriveInQueryUserControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbbType.Bind<OrderChangeType>();
            var com = new List<string>(Domain.DeliveryCompanyHelper.GetDeliveryCompany());
            com.Insert(0, "");
            this.cbbDeliveryCompany.ItemsSource = com;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                long orderId = 0;

                long.TryParse(this.tbOrderId.Text.Trim(), out orderId);
                this.pgBar.Parameters.Clear();

                this.pgBar.Parameters.Add("type", this.cbbType.GetSelectedEnum<OrderChangeType>());
                this.pgBar.Parameters.Add("deliveryCompany", this.cbbDeliveryCompany.Text);
                this.pgBar.Parameters.Add("deliveryNumber", this.tbDeliveryNumber.Text.Trim());
                this.pgBar.Parameters.Add("orderId", orderId);
                this.pgBar.Parameters.Add("goodsInfo", "");
                this.pgBar.Parameters.Add("startTime", this.dpStart.Value == null ? DateTime.MinValue : this.dpStart.Value.Value);
                this.pgBar.Parameters.Add("endTime", this.dpEnd.Value == null ? DateTime.MinValue : this.dpEnd.Value.Value);
                this.pgBar.StartPage();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pgBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var data = this.arriveInDeliveryService.GetByAll(e.GetParameter<OrderChangeType>("type"), e.GetParameter<string>("deliveryCompany"), e.GetParameter<string>("deliveryNumber"), e.GetParameter<long>("orderId"), e.GetParameter<string>("goodsInfo"), e.GetParameter<DateTime>("startTime"), e.GetParameter<DateTime>("endTime"), e.CurrentPage - 1, e.PageSize);
            this.pgBar.Total = data.Total;
            this.pgBar.CurrentCount = data.Datas.Count;
            this.dgvItems.ItemsSource = data.Datas;
        }

    }
}
